package com.common.shared.usermanager;

import java.util.ArrayList;
import java.util.HashMap;

import com.common.shared.SharedClass;
import com.google.gwt.user.client.rpc.IsSerializable;

public class Group extends SharedClass implements IsSerializable {

	private int id;
	private long caption_id;
	private String group_name;
	private ArrayList<Permission> permitions;

	private transient HashMap<Integer, Permission> permitionIds;
	private transient HashMap<String, Permission> permitionNames;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long getCaption_id() {
		return caption_id;
	}

	public void setCaption_id(long caption_id) {
		this.caption_id = caption_id;
	}

	public String getGroup_name() {
		return group_name;
	}

	public void setGroup_name(String group_name) {
		this.group_name = group_name;
	}

	public ArrayList<Permission> getPermitions() {
		if (permitions == null) {
			permitions = new ArrayList<Permission>();
		}
		return permitions;
	}

	public void setPermitions(ArrayList<Permission> permitions) {
		this.permitions = permitions;
	}

	public void removePermition(int permition_id) {
		ArrayList<Permission> permitions = getPermitions();
		for (Permission permition : permitions) {
			if (permition.getId() == permition_id) {
				permitions.remove(permition);
				return;
			}
		}
	}

	public void regeneratePermitions() {
		if (permitionIds == null)
			permitionIds = new HashMap<Integer, Permission>();
		permitionIds.clear();
		if (permitionNames == null)
			permitionNames = new HashMap<String, Permission>();
		ArrayList<Permission> permitions = getPermitions();
		for (Permission permition : permitions) {
			permitionIds.put(permition.getId(), permition);
			permitionNames.put(permition.getPermission_name(), permition);
		}
	}

	public boolean hasPermition(int permitionId) {
		return permitionIds.get(permitionId) != null;
	}

	public boolean hasPermition(String permitionName) {
		return permitionNames.get(permitionName) != null;
	}

	@Override
	public long getIdVal() {
		return getId();
	}

	@Override
	public String getTextVal() {
		return getGroup_name();
	}
}
